﻿//-----------------------------------------------------------------------
// <copyright file="EmailComposeTask.cs" company="In The Hand Ltd">
//     Copyright © 2014 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Text;

namespace InTheHand.Phone.Tasks
{
    /// <summary>
    /// Allows an application to launch the email application with a new message displayed.
    /// Use this to allow users to send email from your application.
    /// </summary>
    [Obsolete("Use InTheHand.ApplicationModel.Email", true)]
    public sealed class EmailComposeTask
    {
        /// <summary>
        /// Shows the email application with a new message displayed.
        /// </summary>
        public void Show()
        {
            // build uri
            StringBuilder sb = new StringBuilder();
            bool firstParameter = true;

            if(string.IsNullOrEmpty(To))
            {
                throw new InvalidOperationException();
            }
            else
            {
                sb.Append("mailto:" + To);
            }

            if (!string.IsNullOrEmpty(Cc))
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("cc=" + Uri.EscapeDataString(Cc));
            }

            if (!string.IsNullOrEmpty(Bcc))
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("bbc=" + Uri.EscapeDataString(Bcc));
            }

            if(!string.IsNullOrEmpty(Subject))
            {
                if(firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("subject=" + Uri.EscapeDataString(Subject));
            }

            if (!string.IsNullOrEmpty(Body))
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("body=" + Uri.EscapeDataString(Body));
            }

            Windows.System.Launcher.LaunchUriAsync(new Uri(sb.ToString()));
        }

        /// <summary>
        /// Gets or sets the recipients on the Bcc line of the new email message.
        /// </summary>
        public string Bcc { set; get; }

        /// <summary>
        /// Gets or sets the body of the new email message.
        /// </summary>
        public string Body { set; get; }

        /// <summary>
        /// Gets or sets the recipients on the Cc line of the new email message.
        /// </summary>
        public string Cc { set; get; }

        /// <summary>
        /// Gets or sets the subject of the new email message.
        /// </summary>
        public string Subject { set; get; }

        /// <summary>
        /// Gets or sets the recipients on the To line of the new email message.
        /// </summary>
        public string To { set; get; }
    }
}
